package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.web.exection.BussionExction;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

@Service
@Transactional

public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;



    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }



    @Override
    public Employee selectOne(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> selectList() {
        List<Employee> employees = employeeMapper.selectAll();
        return employees;
    }

    @Override
    public PageInfo<Employee> query(EmployeeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> data = employeeMapper.queryList(qo);
        return new PageInfo<>(data);
    }

    @Override
    public void insertAll(Long eId, Long rId) {
            employeeMapper.insertAll(eId,rId);
    }

    @Override
    public List selectEmpAndRole(Long id) {
        List list = employeeMapper.selectEmpAndRole(id);
        return list;
    }

    @Override
    public void deleteAll(Long rId) {
        employeeMapper.deleteAll(rId);
    }

    @Override
    public Employee longinCheck(String username, String password) {

        Employee employee = employeeMapper.loginCheck(username);
        if (StringUtil.isEmpty(username)){
            throw new BussionExction("用户名为空");
        }
        if (StringUtil.isEmpty(password)){
            throw new BussionExction("密码为空");
        }
        if (employee == null || !employee.getPassword().equals(password)){
            throw new BussionExction("用户名或密码错误");
        }

        return employee;
    }

    @Override
    public void update(Employee employee, Long ids) {

        employeeMapper.deleteAll(employee.getId());

        if (ids != null){

                employeeMapper.insertAll(employee.getId(),ids);


        }
        employeeMapper.updateByPrimaryKey(employee);
    }

    @Override
    public void insert(Employee employee, Long ids) {
        employeeMapper.insert(employee);

        if (ids != null ){

                employeeMapper.insertAll(employee.getId(),ids);

        }

    }

    @Override
    public List<Permission> userCheck(Long id) {
        List<Permission> employee = employeeMapper.userCheck(id);
        return employee;
    }

    @Override
    public Workbook exportXls(ServletOutputStream outputStream) throws IOException {
        //定义工作表
        Workbook wb = new HSSFWorkbook();

        //定义工作页
        Sheet sheet = wb.createSheet();

        //定义行
        Row row = sheet.createRow(0);

        //定义列
        row.createCell(0).setCellValue("员工编号");
        row.createCell(1).setCellValue("员工姓名");
        row.createCell(2).setCellValue("员工邮箱");


        List<Employee> employees = employeeMapper.selectAll();

        System.out.println(employees.size());
        for (int i = 0; i < employees.size(); i++) {
            Employee e = employees.get(i);
            Row row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(e.getId());
            row1.createCell(1).setCellValue(e.getName());
            row1.createCell(2).setCellValue(e.getEmail());

        }
        wb.write(outputStream);
        return wb;
    }

    @Override
    public void inputXml(MultipartFile file) throws IOException {

        //把接收到的文件以excel的方式去读取并操作
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //读取第一页
        Sheet sheet = wb.getSheetAt(0);
        //获取最后一行的索引
        int lastRowNum = sheet.getLastRowNum();

        //从索引为1的行数开始读
        for (int i = 1; i <= lastRowNum; i++) {
            //获取行数据
            Row row = sheet.getRow(i);

            Double id = row.getCell(0).getNumericCellValue();
            //判断如果用户名是空，就不再往下读
    /*        if(!StringUtils.hasLength(id.trim())){
                return;
            }*/
            Employee employee = new Employee();

            employee.setEmail(row.getCell(2).getStringCellValue());
            employee.setName(row.getCell(1).getStringCellValue());
            Double numericCellValue = row.getCell(3).getNumericCellValue();
            employee.setAge(numericCellValue.intValue());
            employee.setId(id.longValue());

            //设置默认密码1
            employee.setPassword("11111");
            //调用保存方法
            employeeMapper.insert(employee);

        }
    }

    @Override
    public List<Employee> selectName(String keyword) {
        List<Employee> employees = employeeMapper.selectName(keyword);
        return employees;
    }


}
